package cst.zyfypt_917cqy.zyfypt917cqy;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends AppCompatActivity {
    private EditText username;
    private EditText password;
    private Button btn_login;
    private Button btn_reg;
    private ResModel model;
    private AmicoolListener<LoginBean> listener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(cst.zyfypt_917cqy.zyfypt917cqy.R.layout.activity_login);
        username=(EditText)findViewById(cst.zyfypt_917cqy.zyfypt917cqy.R.id.login_username);
        password=(EditText)findViewById(cst.zyfypt_917cqy.zyfypt917cqy.R.id.login_password);
        btn_login=(Button) findViewById(cst.zyfypt_917cqy.zyfypt917cqy.R.id.btn_login);
        btn_reg=(Button) findViewById(cst.zyfypt_917cqy.zyfypt917cqy.R.id.btn_reg);
        listener=new AmicoolListener<LoginBean>() {
            @Override
            public void onResponse(LoginBean l) {
                if(l.getUsername()!=null){
                    String sessionid=l.getSessionid();//把sessionid存到一个地方
                    Intent intent=new Intent(
                            LoginActivity.this,MainActivity.class);
                    startActivity(intent);
                    finish();

                }
                else{
                    Toast.makeText(LoginActivity.this,"用户名或密码错误",Toast.LENGTH_SHORT).show();

                }

            }

            @Override
            public void onFail(String msg) {
                Toast.makeText(LoginActivity.this,msg,Toast.LENGTH_SHORT).show();

            }
        };
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                model=new ResModel("gson");
                model.login(username.getText().toString(),password.getText().toString(), listener);
            }
        });


    }
}
